package com.smithlibraties.util.annotation.register.loader;

import com.smithlibraties.SmithLibraries;
import com.smithlibraties.util.annotation.AnnotationClassGetter;
import com.smithlibraties.util.annotation.register.RegisterTileEntity;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.tileentity.TileEntity;

import java.util.ArrayList;
import java.util.List;

public class TileEntityRegister {
    List<List<TileEntityAttribute>> list = new ArrayList<>();//每个类为一个List，然后吧每个类的List集中到这个List

    public TileEntityRegister() {
        List<Class> tileEntityCollections = AnnotationClassGetter.getAllClassByInterface(SmithLibraries.getAllClasses(), RegisterTileEntity.class);//获取实现RegisterClass注释的类的List
        for (Class clazz : tileEntityCollections) {//开始集中继承IRegister接口的的类的属性的List
            list.add(TileEntityRegister.getAnnotationList(clazz));
        }
        for (List<TileEntityAttribute> attributeList : list) {
            for (TileEntityAttribute registerAttribute : attributeList) {
                registerTileEntity((Class<? extends TileEntity>) registerAttribute.getM(), registerAttribute.getId());
            }
        }
    }

    private static void registerTileEntity(Class<? extends TileEntity> tileEntityClass, String id) {
        GameRegistry.registerTileEntity(tileEntityClass, id);
    }

    private static List<TileEntityAttribute> getAnnotationList(Class clazz) {
        List<TileEntityAttribute> list1 = new ArrayList<>();
        if (clazz.isAnnotationPresent(RegisterTileEntity.class)) {
            RegisterTileEntity registerTileEntity = (RegisterTileEntity) clazz.getAnnotation(RegisterTileEntity.class);
            list1.add(new TileEntityAttribute(clazz, registerTileEntity.id()));
        }
        return list1;
    }
}
